﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PloobsEngine.SceneControl;

namespace Etapa1.SteeringExperiments
{
    public enum Deceleration
    {
        slow = 4,
        normal = 3,
        fast = 2,
        fastest = 1
    };

    public class Arrive : Steering
    {
        public Arrive(Vector3 target, Deceleration deceleration, float decelerationBeginsAtDistance)
            : base(BehaviorType.arrive)
        {
            this._deceleration = deceleration;
            this._decelerationBeginsAtDistance = decelerationBeginsAtDistance;
            this.target = target;
            isObj = false;
        }

        public Arrive(IObject target, Deceleration deceleration, float decelerationBeginsAtDistance)
            : base(BehaviorType.arrive)
        {
            this._deceleration = deceleration;
            this._decelerationBeginsAtDistance = decelerationBeginsAtDistance;
            this.objTarget = target;
            isObj = true;
        }

        private Vector3 target;
        private IObject objTarget;
        bool isObj;
        private Deceleration _deceleration;        

        public Deceleration Deceleration
        {
            get { return _deceleration; }
            set { _deceleration = value; }
        }

        private float _decelerationBeginsAtDistance;

        public float DecelerationBeginsAtDistance
        {
            get { return _decelerationBeginsAtDistance; }
            set { _decelerationBeginsAtDistance = value; }
        }
        

        public Vector3 Target
        {
            get { return target; }
            set { target = value; isObj = false; }
        }        

        public IObject ObjTarget
        {
            get { return objTarget; }
            set { objTarget = value; isObj = true; }
        }

        bool freiando = false;

        public override Microsoft.Xna.Framework.Vector3 CalculateSteering(SteeringEntityInfo entity)
        {

            Vector3 ToTarget = Vector3.Zero;
            if (isObj == true)
            {
                ToTarget = objTarget.Position - entity.Obj.Position;
            }
            else
            {
                ToTarget = target - entity.Obj.Position;
            }
            
            float distance = ToTarget.Length();

            Vector3 DesiredVelocity = Vector3.Zero;
                        
            if (distance <= _decelerationBeginsAtDistance)
            {
                    const float DecelerationTweaker = 0.5f;                
                    float speed = distance / ((float)_deceleration * DecelerationTweaker);
                
                    speed = MathHelper.Min(speed, entity.MaxVelocity);

                    ToTarget.Normalize();
                
                    DesiredVelocity = ToTarget * speed;
                
            }
            else
            {
                freiando = false;
                // Seek normal
                if (isObj == true)
                {
                    DesiredVelocity = Vector3.Normalize(objTarget.Position - entity.Obj.Position)
                             * entity.MaxVelocity;
                }
                else
                {
                    DesiredVelocity = Vector3.Normalize(target - entity.Obj.Position)
                             * entity.MaxVelocity;
                }
            }            

            return (DesiredVelocity - entity.Obj.PhysicObject.Velocity);
        }
    }
}
